package com.example.xunua_today_in_history;

import androidx.activity.OnBackPressedCallback;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xunua_today_in_history.Adapter.HistoryAdapter;
import com.example.xunua_today_in_history.bean.HistoryBean;

import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class HistoryActivity extends AppCompatActivity {
    private ImageView img_back;
    private ListView lv;
    private HistoryBean historyBean;
    private ArrayList<HistoryBean.ResultBean> mDatas;
    private HistoryAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        initView();
    }

    private void initView() {
        img_back=findViewById(R.id.history_img_back);
        lv=findViewById(R.id.history_lv);
        historyBean=new HistoryBean();
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mDatas=new ArrayList<>();
        adapter=new HistoryAdapter(mDatas,HistoryActivity.this);
        lv.setAdapter(adapter);
        lv.addFooterView(LayoutInflater.from(this).inflate(R.layout.item_footerview,null));
        Intent intent = getIntent();
        Bundle bundle=intent.getExtras();
        historyBean = (HistoryBean) bundle.getSerializable("history");
        List<HistoryBean.ResultBean> list=historyBean.getResult();
        mDatas.addAll(list);
        adapter.notifyDataSetChanged();

        //为ListView设置点击事件
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position==mDatas.size()){    //如mdata.size()=36  则数据是从0-35      listview则是mdata.size+1=37  则是0-36索引   点击尾部view则是36  36=mdata.size  把自己弄糊涂了。
                    return;
                }
                Intent intent = new Intent(HistoryActivity.this, HistoryDescActivity.class);
                HistoryBean.ResultBean resultBean = mDatas.get(position);
                String his_id = resultBean.get_id();
                intent.putExtra("hisId",his_id);
                startActivity(intent);
            }
        });
    }
    @Override
    public void onBackPressed() {
//        super.onBackPressed();   阻止返回键
    }
}
